package com.example.huawei.system.controller;

import com.example.huawei.system.entity.CarInfo;
import com.example.huawei.system.result.Result;
import com.example.huawei.system.service.CarInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "车辆信息接口")
@RestController
@RequestMapping("/carInfo")
public class CarInfoController {
    @Autowired
    private CarInfoService carInfoService;

//    @ApiOperation(value = "分页查询车辆信息", notes = "分页查询车辆信息")
//    @GetMapping("/page")
//    public Result queryPage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
//                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
//        Page<CarInfo> page = new Page<>(pageNo, pageSize);
//        return Result.ok(carInfoService.page(page));
//    }

    @ApiOperation(value = "列表查询车辆信息", notes = "列表查询车辆信息")
    @GetMapping("/list")
    public Result<List<CarInfo>> queryList() {
        return Result.ok(carInfoService.list());
    }


    @ApiOperation(value = "新增车辆信息", notes = "新增车辆信息")
    @PostMapping("/add")
    public Result<?> add(@RequestBody CarInfo carInfo) {
        CarInfo one = carInfoService.lambdaQuery().eq(CarInfo::getCarName, carInfo.getCarName()).one();
        if (one != null) {
            return Result.error().message("已存在该车辆,请勿重复添加");
        }
        carInfo.setCreateTime(new Date());
        boolean flag = carInfoService.save(carInfo);
        return flag ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "修改车辆信息", notes = "修改车辆信息")
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody CarInfo carInfo) {
        boolean flag = carInfoService.updateById(carInfo);
        return flag ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "删除车辆信息", notes = "删除车辆信息")
    @DeleteMapping("/del/{id}")
    public Result<?> delete(@PathVariable("id") String id) {
        boolean flag = carInfoService.removeById(id);
        return flag ? Result.ok() : Result.error();
    }
}
